<%+header%>
<!--
    This module is a demo to configure MTK' proprietary WiFi driver.
    Basic idea is to bypass uci and edit wireless profile (mt76xx.dat) directly.
    LuCI's WiFi configuration is more logical and elegent, but it's quite tricky to 
    translate uci into MTK's WiFi profile (like we did in "uci2dat").

    Hua Shao <nossiac@163.com>
-->
<%
local mtkwifi = require("mtkwifi")
local devs = mtkwifi.get_all_devs()
%>
    <h2><a name="content">无线概况</a></h2>

    <div id="loading" style="margin-top: 1em; display: none;">
    <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:e;" />
        &nbsp;&nbsp;&nbsp; Please waiting while the page is loading......
    </div>

    <% if #devs == 0 then %>
    <fieldset class="cbi-section">
        <legend>No wireless device found.</legend>
    </fieldset>
    <% end %>

    <% for _,dev in ipairs(devs) do %>
    <fieldset class="cbi-section">
        <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
            <!-- physical device -->
            <tbody>
                <tr>
                    <td style="width:34px">
                        <img src="/luci-static/resources/icons/wifi_big.png" style="float:left; margin-right:10px" />
                    </td>
                    <td colspan="2" style="text-align:left">
                        <big><strong title="<%=dev.profile%>"> Generic Mediatek <%=dev.devname%></strong></big>
                        <%
                        local diff = mtkwifi.diff_profile(dev.profile)
                        if next(diff) ~= nil then
                        %>
                            <span style="color:red;">* need reload to apply changes</span>
                        <% 
                        end
                        %>
                        <br/>
                        <span><strong>模式:</strong>  <% if dev.ApCliEnable == "1" then %> APCli <% else %> Master AP<% end %> </span>
                    </td>
                    <td style="width:310px;text-align:right">
                        <input class="cbi-button cbi-button-reload" style="width:100px" title="Reload WiFi network" value="重新加载" type="button" onclick='wifi_reload("<%=luci.dispatcher.build_url("admin", "network", "wifi", "reload", dev.devname)%>");' />
                        <input class="cbi-button cbi-button-edit" style="width:100px" title="Configure WiFi device" value="修改" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "dev_cfg_view", dev.devname)%>'" />
                        <input class="cbi-button cbi-button-add" style="width:100px" title="Add WiFi network" value="添加" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_add_view", dev.devname,dev.vifs.__prefix)%>'" />
                    </td>
                </tr>
                <!-- /physical device -->
                <!-- network list -->
                <% for _,vif in ipairs(dev.vifs) do %>
                <tr class="cbi-section-table-row cbi-rowstyle-1">
                    <td></td>
                    <td class="cbi-value-field" style="width:16px; padding:3px">
                        <% if vif.state == "up" then %>
                        <img src="/luci-static/resources/icons/signal-75-100.png" />
                        <% else %>
                        <img src="/luci-static/resources/icons/signal-none.png" />
                        <% end %>
                    </td>
                    <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px"><strong>Interface:</strong> <%=vif.vifname%> | <strong>SSID:</strong> <%=vif.__ssid%> | <strong>信道:</strong> <%=vif.__channel or dev.Channel%> 
                        <br/>
                        <% if vif.state == "up" then %>
                        <strong>BSSID:</strong>:<%=vif.__bssid%> | <strong>无线模式:</strong> <%=dev.WirelessModeList[tonumber(vif.__wirelessmode or dev.WirelessMode)]%>
                        <% else %>
                        Wireless is disabled or not associated
                        <% end %>
                    </td>
                    <td class="cbi-value-field" style="text-align:right">
                        <% if not vif.state then %>
                        <input class="cbi-button cbi-button-apply" style="width:100px" title="Enable this network" value="启用" type="button" onclick="alert('Please restart manually to create this interface.')" />
                        <% elseif vif.state == "up" then %>
                        <input class="cbi-button cbi-button-reset" style="width:100px" title="Disable this network" value="停用" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_disable", vif.vifname)%>'" />
                        <% else %>
                        <input class="cbi-button cbi-button-apply" style="width:100px" title="Enable this network" value="启用" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_enable", vif.vifname)%>'" />
                        <% end %>
                        <input class="cbi-button cbi-button-edit" style="width:100px" title="Edit this network" value="修改" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_cfg_view", dev.devname, vif.vifname)%>'" />
                        <input class="cbi-button cbi-button-remove" style="width:100px" title="Delete this network" value="移除" type="button" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_del", dev.devname, vif.vifname)%>'" />
                    </td>
                </tr>
                <% end %>
                <!-- /network list -->
            </tbody>
        </table>
    </fieldset>
    <% end %>

<!--
    <h2><a id="content" name="content"><%:Station List%></a></h2>

    <fieldset class="cbi-section">
        <table class="cbi-section-table" style="margin:10px" id="iw-assoclist">
            <tr class="cbi-section-table-titles">
                <th class="cbi-section-table-cell"></th>
                <th class="cbi-section-table-cell"><%:SSID%></th>
                <th class="cbi-section-table-cell"><%:MAC-Address%></th>
                <th class="cbi-section-table-cell"><%:IPv4-Address%></th>
                <th class="cbi-section-table-cell"><%:Signal%></th>
                <th class="cbi-section-table-cell"><%:Noise%></th>
                <th class="cbi-section-table-cell"><%:RX Rate%></th>
                <th class="cbi-section-table-cell"><%:TX Rate%></th>
            </tr>
            <tr class="cbi-section-table-row cbi-rowstyle-2">
                <td class="cbi-value-field" colspan="8">
                    <em><%:Collecting data...%></em>
                </td>
            </tr>
        </table>
    </fieldset>
-->

<script type="text/javascript">
function wifi_reload(url) {
    window.scrollTo(0, 0);
    document.getElementById('loading').style.display="";
    XHR.get(url, null,
        function(x)
        {
            console.log(x);
            document.getElementById("loading").style.display="none";
            window.location.reload();
        }
    );
}
</script>

<%+footer%>
